//Render.js
function render_load(src) {
    render.source = src;
    //Load the JSON Object file
    //Then put all tris and spheres
    //Into polygons
    //--------------------

    //--------------------
};

function default_scene() {
    //Load the default scene
    var Ka = new rgbcolor(0.1, 0.1, 0.1, 1);
    var Kd = new rgbcolor(1, 0.843, 0.2, 1);
    var Ks = new rgbcolor(0.5, 0.2, 0.2, 1);

    var sp = new type_sphere(new coord(200,200,500), 90);
    var mater = new type_material(Ka, Kd, Ks, 32);
    sp.material = mater;
    sp.material.ref = true;
    sp.transparent = false;
    sp.material.ref_ratio = Ks;

    polygons.sphere[polygons.sphere.length] = sp;
    
    /*
    var Ka = new rgbcolor(0.1, 0.1, 0.1, 1);
    var Kd = new rgbcolor(1, 0.843, 0.2, 1);
    var Ks = new rgbcolor(0.5, 0.2, 0.2, 1);
    
    var sp = new type_sphere(new coord(200,200,800), 70);
    var mater = new type_material(Ka, Kd, Ks, 32);
    sp.material = mater;
    sp.material.ref = true;
    sp.transparent = false;
    sp.material.ref_ratio = Ks;
	
    polygons.sphere[polygons.sphere.length] = sp;
	*/
	
    sp = new type_sphere(new coord(400,250,400), 50);
    sp.material = new type_material(Ka,Kd,Ks, 40);
    sp.material.ref = false;
    sp.material.transparent = true;
    sp.material.ref_ratio = Ks;

    polygons.sphere[polygons.sphere.length] = sp;
    sp.material.opaque = 0.6;
    sp.material.refr_idx = 1.5;

    var plane = new type_plane(new coord(-200, -100, 1000),
			       vec_normalize(new coord(0,-1,-1)),
			       vec_normalize(new coord(-0.5,1,-1)),
			       -1, -1);
    polygons.plane[polygons.plane.length] = plane;
    mater = new type_material(new rgbcolor(0,0,0,1),
			      new rgbcolor(0.4,0.4,0.4,1),
			      new rgbcolor(0.5,0.4,0.3,1),
			      5);
    plane.material = mater;
    mater.ref = true;
    mater.transparent = false;
    mater.ref_ratio = new rgbcolor(0.4, 0.4, 0.4, 1);
    
    plane.text = new type_texture(null, 0, 0);
    plane.text.get_color = set_chessboard(plane);
    
    var l = new type_light(500,-500,200,new rgbcolor(255,255,255,1));
    lights[lights.length] = l;

    //filter.type = 1;
    ambient_light = new rgbcolor(50,50,50,1);
    display.bgcolor = new rgbcolor(0,0,0,1);
};



function render_display() {
    //For every pixel, track the light
    
    var d = 1/Math.tan(camera.FOV/360*constants.PI);
    var dir = new coord(0,0,d);
    var position = new coord(0,0,0);
    var t_ray = new type_ray(position, dir);
    
    /*edited by Shizhi Liu*/
    // super sampling factor.
    var AAA = render.ANTI_ALIASING_FACTOR;
    
    // multipass antialiasing matrix.
    var filter = render.AAFilter;
    var isMultiPass = render.isMultiPass;
    
    var xStart = render.ray_trace.xres/2 - display.xres/2;
	var yStart = render.ray_trace.yres/2 - display.yres/2;
    
    for(var i = 0; i < display.xres; i+=render.ray_trace.xres) {
    
    var xLoc = xStart + i;
    
	for(var j = 0; j < display.yres; j+=render.ray_trace.yres) {
	
		var yLoc = yStart + j;
		
		var xStartA = xLoc - (AAA - 1)*render.ray_trace.xres/2.0;
        var yStartA = yLoc - (AAA - 1)*render.ray_trace.yres/2.0;
        
        var xPixLenA = render.ray_trace.xres/AAA;
        var yPixLenA = render.ray_trace.yres/AAA;
        
        var color = new rgbcolor(0,0,0,1);
        
        for (var iA = 0; iA < AAA; iA++) {
                
            for (var jA = 0; jA < AAA; jA++) {
            
            	var xLocA = xStartA + iA*xPixLenA;                    
                var yLocA = yStartA + jA*yPixLenA;
	    			
	    		position.x = xLocA + display.xres/2;
	    		position.y = yLocA + display.yres/2;
            	
            	if (isMultiPass) {	
            		
            		var colorA = new rgbcolor(0,0,0,1);
            	
            		for (var pass = 0; pass < filter.length; pass++) {
                    
                		dir.x = (xLocA + filter[pass][0]*render.ray_trace.xres)/display.xres*2;
	    				dir.y = (yLocA + filter[pass][1]*render.ray_trace.yres)/display.yres*2;
	    		
	    				var ray_dir = vec_normalize(dir);
	    		
	    				t_ray.vertex = position;
	    				t_ray.norm = ray_dir;
	    		
	    				var colorM = ray_tracer(t_ray, render.ray_trace.trace_depth);
                    
                		colorA.r += colorM.r;
                		colorA.g += colorM.g;
                		colorA.b += colorM.b;
                		colorA.a += colorM.a;            		
            		}
            		
	    			colorA.r /= filter.length;
	    			colorA.g /= filter.length;
	    			colorA.b /= filter.length;
	    			colorA.a /= filter.length;            		            		
            	}
            	
            	else {
                    
                	dir.x = xLocA/display.xres*2;
	    			dir.y = yLocA/display.yres*2;
	    		
	    			var ray_dir = vec_normalize(dir);
	    		
	    			t_ray.vertex = position;
	    			t_ray.norm = ray_dir;
	    		
	    			var colorA = ray_tracer(t_ray, render.ray_trace.trace_depth);
            	}
            	
            	color.r += colorA.r;
                color.g += colorA.g;
                color.b += colorA.b;
                color.a += colorA.a;
            }
        }
        
	    color.r /= AAA*AAA;
	    color.g /= AAA*AAA;
	    color.b /= AAA*AAA;
	    color.a /= AAA*AAA;

	    for(var p = 0; p < render.ray_trace.xres; ++p) {	    
			for(var q = 0; q < render.ray_trace.yres; ++q) {		
		    	putdisp(i+p,j+q,color);		    	
			}		
	    }
	    
	}
    }
    
    //If any post processing, do it
    for(var k = 0; k < render.post_process.length; ++k) {
	render.post_process[k]();
    }
    flushdisp();
};